
#define addedHeight 60.0f

#import "AppDelegate.h"
#import "QuestionsViewController.h"
#import "MapViewController.h"
#import "DetailsViewController.h"
#import "APIDownload.h"
#import "CJSONDeserializer.h"
#import "QuestionCell.h"
#import "Question.h"
#import "Downloader.h"

@interface QuestionsViewController () <UIActionSheetDelegate,UITableViewDelegate, UITableViewDataSource>
@property (nonatomic,retain) NSArray *questions;
@end

@implementation QuestionsViewController

@synthesize qTable;

-(void) dealloc;
{
    self.qTable = nil;
    [super dealloc];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"Questions";
        self.tabBarItem.image = [UIImage imageNamed:@"question"];
        
    }
    return self;
}

-(void)finishedLoading:(NSArray*)questionList{
    self.questions = questionList;
    [qTable reloadData];
}

- (void)openFilter
{    
    UIActionSheet *actSheet = [[[UIActionSheet alloc] initWithTitle:@"Заголовок" 
                                                          delegate:self 
                                                 cancelButtonTitle:@"Кнопка отмены" 
                                            destructiveButtonTitle:nil 
                                                 otherButtonTitles:nil] autorelease];
    actSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
    [actSheet showFromBarButtonItem:self.navigationController.topViewController.navigationItem.leftBarButtonItem animated:YES];
}

- (void)openMap
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.50];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.navigationController.view cache:NO];
    MapViewController *map =  [[[MapViewController alloc]init]autorelease ];
    [self.navigationController pushViewController:map animated:NO] ;
    [UIView commitAnimations];

}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
	self.navigationController.topViewController.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Filter" style:UIBarButtonItemStyleBordered target:self action:@selector(openFilter)] autorelease];
    
    self.navigationController.topViewController.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"Map" style:UIBarButtonItemStyleBordered target:self action:@selector(openMap)] autorelease];
    
    UITableView *table=[[[UITableView alloc]initWithFrame:CGRectMake(0,0,self.view.bounds.size.width,self.view.bounds.size.height - self.tabBarController.tabBar.bounds.size.height - self.navigationController.navigationBar.bounds.size.height)]autorelease ];
    table.delegate = self;
    table.dataSource = self;
    table.separatorColor = [UIColor blackColor];
    [self.view addSubview:table];
    self.qTable = table;
    
    Downloader *helper = [Downloader new];
    helper.delegate = self;
    helper.managedObjectContext = self.managedObjectContext;
    [helper getQuestionsList];
    
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.questions.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%d",indexPath.row);
    static NSString *CellIdentifier = @"Cell";
    
    QuestionCell *cell = (QuestionCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"QuestionCell"owner:self options:nil];
        cell = [nib objectAtIndex:0];   
        }
    if(indexPath.row == 4) NSLog(@"%@",self.questions);
    NSLog(@"index path for question - %d question array count - %d",indexPath.row,self.questions.count);
    Question* question = [self.questions objectAtIndex:indexPath.row];
    
    cell.question = question;

    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{   

    Question* question = [self.questions objectAtIndex:indexPath.row];
    NSString *cellText = question.questionText;
    UIFont *cellFont = [UIFont systemFontOfSize:16.0f];
    CGSize constraintSize = CGSizeMake(245.0f, MAXFLOAT);
    
    CGSize labelSize = [cellText sizeWithFont:cellFont 
                            constrainedToSize:constraintSize 
                                lineBreakMode:UILineBreakModeWordWrap];
    
    return labelSize.height + addedHeight;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DetailsViewController *detailViewController = [[DetailsViewController new]autorelease];
    detailViewController.question = [self.questions objectAtIndex:indexPath.row];
    detailViewController.managedObjectContext = self.managedObjectContext;
    detailViewController.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:detailViewController animated:YES];
    
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
@end
